package com.yiren.backstage.player.services;

import android.content.Context;
import android.media.MediaPlayer;

import com.yiren.backstage.currency.bean.VoiceBean;
import com.yiren.backstage.currency.utils.FileUtil;
import com.yiren.backstage.currency.utils.StringUtil;
import com.yiren.backstage.player.announcer.AbstractMediaPlayer;
import com.yiren.backstage.player.announcer.QueueMediaPlayer;
import com.yiren.backstage.player.announcer.SingleMediaPlayer;
import com.yiren.backstage.player.api.IPlayerCallBack;
import com.yiren.backstage.player.api.ITtsSynthesisCallBack;
import com.yiren.backstage.player.constant.PlayerConstant;
import com.yiren.backstage.player.utils.LameUtil;
import com.yiren.backstage.player.utils.TtsUtil;

import java.io.File;

/**
 * Time:2021/12/29 11:34
 * Author:xiaozan
 * Description:语音播报服务
 */
public class PlayVoiceService {

    private static TtsUtil ttsUtil;

    private static LameUtil lameUtil;

    public static TtsUtil getTtsUtil(Context context) {
        if (null == ttsUtil) {
            ttsUtil = new TtsUtil(context);
            lameUtil = new LameUtil(context);
        }
        return ttsUtil;
    }

    /**
     * 返回是讯飞语音合成播报 还是 播报器播报
     * @param voiceBean
     * @return
     */
    private static boolean isTtsPlayVoice(VoiceBean voiceBean) {
        try {
            String path = returnVoicePath(voiceBean);
            File file = new File(path);
            if (file.exists()) return false;/*{
                //判断语音文件是否存在，如果不存在并且是语音流  将语音流生成文件
                if (voiceFile.getVoiceValueType().equals(VoiceValueTypeEnum.VOICE.toString()) && null != voiceFile.getVoiceFileData()) {
                    FileUtil.saveFile(voiceFile.getVoiceFileData(), path);
                    return false;
                } else {
                    return true;
                }
            } else {
                return false;
            }*/
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * 播报语音 语音本地不存在就讯飞语音合成后再播报
     *
     * @param context
     * @param voiceBean
     * @param iPlayerCallBack
     * @param abstractMediaPlayer 播报器
     */
    public static void mediaPlayVoice(Context context, VoiceBean voiceBean, final IPlayerCallBack iPlayerCallBack, final AbstractMediaPlayer abstractMediaPlayer) {
        try {
            final String path = returnVoicePath(voiceBean);
            if (isTtsPlayVoice(voiceBean)) {
                voiceBean.setVoiceContent(voiceBean.getVoiceContent().replaceAll("\n", "").trim());
                //等待语音合成后再播报
                getTtsUtil(context).synthesizeToUri(voiceBean.getVoiceContent(), new ITtsSynthesisCallBack() {
                    @Override
                    public void synthesisFinish() {
                        String pcmPath = path.replace(".wav", ".pcm");
                        //将pcm格式转换成wav格式
                        lameUtil.pcm2wav(pcmPath, path);
                        //删除pcm文件
                        FileUtil.deleteFile2(pcmPath);
                        abstractMediaPlayer.playSound(path, new MediaPlayer.OnCompletionListener() {
                            @Override
                            public void onCompletion(MediaPlayer mediaPlayer) {
                                if (null != iPlayerCallBack) {
                                    iPlayerCallBack.playFinish();
                                }
                            }
                        });
                    }
                });
            } else {
                abstractMediaPlayer.playSound(path, new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mediaPlayer) {
                        if (null != iPlayerCallBack) {
                            iPlayerCallBack.playFinish();
                        }
                    }

                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 返回语音路径  如果语音内容类型是：语音文件，名字直接包含后缀名
     * @param voiceBean
     * @return
     */
    private static String returnVoicePath(VoiceBean voiceBean) {
        String path = "";
        if (!StringUtil.isEmpty(voiceBean.getVoiceFileName())) {
            path = PlayerConstant.VOICE_SAVE_DIRECTORY + voiceBean.getVoiceFileName();
        } else {
            path = PlayerConstant.VOICE_SAVE_DIRECTORY + voiceBean.getVoiceContent() + PlayerConstant.WAV;
        }
        return path;
    }


    /**
     * 重置播报器
     */
    public static void resetAllPlayer() {
        QueueMediaPlayer.getInstance().reset();
        SingleMediaPlayer.getInstance().reset();
        if (null != ttsUtil) ttsUtil.stopSpeaking();
    }

    /**
     * 释放播报器
     */
    public static void releaseAllPlayer() {
        QueueMediaPlayer.getInstance().release();
        SingleMediaPlayer.getInstance().release();
        if (null != ttsUtil) ttsUtil.stopSpeaking();
    }
}
